package com.ics.cmsadmin.modules.basic.controller;

import com.ics.cmsadmin.frame.core.annotation.Authorize;
import com.ics.cmsadmin.frame.core.bean.ApiResponse;
import com.ics.cmsadmin.frame.core.bean.PageBean;
import com.ics.cmsadmin.frame.core.enums.ApiResultEnum;
import com.ics.cmsadmin.frame.core.enums.AuthorizeEnum;
import com.ics.cmsadmin.frame.core.exception.CmsException;
import com.ics.cmsadmin.modules.auth.service.AccessService;
import com.ics.cmsadmin.modules.basic.bean.StudentBean;
import com.ics.cmsadmin.modules.basic.bean.VideoHistoryBean;
import com.ics.cmsadmin.modules.basic.service.StudentService;
import com.ics.cmsadmin.modules.basic.service.VideoHistoryService;
import com.ics.cmsadmin.modules.sso.utils.SsoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * t_student controller
 * Created by lvsw on 2018-52-22 19:09:32.
 */
@Api(description = "学生管理")
@RestController("newStudentController")
@RequestMapping("/student")
public class StudentController {

    @Resource(name = "newStudentService")
    private StudentService studentService;
    @Resource
    private AccessService accessService;
    @Resource
    private VideoHistoryService videoHistoryService;

    @Authorize(AuthorizeEnum.STUDENT_QUERY)
    @ApiOperation(value = "查询信息")
    @GetMapping(value = "/query/{id}")
    public ApiResponse queryById(@ApiParam(value = "id") @PathVariable String id){
        return new ApiResponse(studentService.queryById(id));
    }

    @Authorize(AuthorizeEnum.STUDENT_QUERY)
    @ApiOperation(value = "分页查询信息")
    @PostMapping("/list/{pageNo}/{pageSize}")
    public ApiResponse list(@RequestBody StudentBean studentBean,
                            @ApiParam("页码") @PathVariable int pageNo,
                            @ApiParam("每页条数") @PathVariable int pageSize,
                            HttpServletRequest request){
        PageBean pageBean = new PageBean(pageNo, pageSize);
        String loginUserId = SsoUtils.getLoginUserId(request);
        return new ApiResponse(studentService.listByLoginUserId(studentBean, loginUserId, pageBean));
    }



    @ApiOperation(value = "分页查询学生关联视频信息")
    @Authorize(AuthorizeEnum.STUDENT_VIDEO_QUERY)
    @PostMapping("/listViewVideo/{pageNo}/{pageSize}")
    public ApiResponse list(@RequestBody VideoHistoryBean bean,
                            @ApiParam("页码") @PathVariable Integer pageNo,
                            @ApiParam("每页条数")  @PathVariable Integer pageSize) {
        return ApiResponse.getDefaultResponse(videoHistoryService.list(bean, new PageBean(pageNo, pageSize)));
    }

    @ApiOperation(value = "更新学生信息")
    @Authorize(AuthorizeEnum.STUDENT_UPDATE)
    @PostMapping("/update/{id}")
    public ApiResponse update(@RequestBody StudentBean student,
                              @PathVariable String id,
                              HttpServletRequest request) {
        if (!accessService.canAccessForStudent(SsoUtils.getLoginUserId(request), id)){
            return new ApiResponse(ApiResultEnum.VALIDATE_ERROR, "您无权限操作该学生");
        }
        return ApiResponse.getDefaultResponse(studentService.update(id, student));
    }

    @ApiOperation(value = "更新学生信息")
    @Authorize(AuthorizeEnum.STUDENT_UPDATE)
    @PostMapping("/vip/update/{id}")
    public ApiResponse updateVIP(@RequestBody StudentBean student,
                              @PathVariable String id,
                              HttpServletRequest request) {
        if (!accessService.canAccessForStudent(SsoUtils.getLoginUserId(request), id)){
            return new ApiResponse(ApiResultEnum.VALIDATE_ERROR, "您无权限操作该学生");
        }
        return ApiResponse.getDefaultResponse(studentService.updateVip(id, student));
    }
}
